﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace sy4_5
{
    public partial class Form1 : Form
    {
        private Image originalImage;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initHScrollBar();
            initPicture();
        }

        private void initHScrollBar()
        {
            hScrollBar1.Minimum = 10;
            hScrollBar1.Maximum = 200;
            hScrollBar1.SmallChange = 5;
            hScrollBar1.LargeChange = 20;
            hScrollBar1.Value = 100;
            hScrollBar1.Dock = DockStyle.Bottom;
        }

        private void initPicture()
        {
            try
            {
                // 方法1：使用测试图片
                CreateTestImage();

                // 方法2：如果要使用真实图片，取消注释下面这行并修改路径
                // originalImage = Image.FromFile(@"你的图片路径");

                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Dock = DockStyle.Fill;

                ZoomPicture(hScrollBar1.Value);
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载图片失败: " + ex.Message);
                CreateTestImage();
            }
        }

        private void CreateTestImage()
        {
            Bitmap testImage = new Bitmap(400, 300);
            using (Graphics g = Graphics.FromImage(testImage))
            {
                g.Clear(Color.LightBlue);
                g.DrawRectangle(new Pen(Color.Blue, 3), 50, 50, 300, 200);
                g.DrawString("图像缩放测试程序\n拖动滚动条缩放图片",
                            new Font("微软雅黑", 12),
                            Brushes.Black,
                            new PointF(80, 120));
            }
            originalImage = testImage;
        }

        private void ZoomPicture(int zoomPercent)
        {
            if (originalImage == null) return;

            try
            {
                float scale = zoomPercent / 100.0f;
                int newWidth = (int)(originalImage.Width * scale);
                int newHeight = (int)(originalImage.Height * scale);

                Bitmap zoomedImage = new Bitmap(newWidth, newHeight);
                using (Graphics g = Graphics.FromImage(zoomedImage))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(originalImage, 0, 0, newWidth, newHeight);
                }

                if (pictureBox1.Image != null)
                    pictureBox1.Image.Dispose();

                pictureBox1.Image = zoomedImage;
                this.Text = $"图像缩放程序 - 当前缩放: {zoomPercent}%";
            }
            catch (Exception ex)
            {
                MessageBox.Show("缩放图片时出错: " + ex.Message);
            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            ZoomPicture(hScrollBar1.Value);
        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            ZoomPicture(hScrollBar1.Value);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (hScrollBar1 != null)
            {
                ZoomPicture(hScrollBar1.Value);
            }
        }
    }
}